using System;
using System.Data;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TanashCMS.Business;

/// <summary>
/// Summary description for SmartPageBase
/// </summary>
public abstract class SmartPageBase : System.Web.UI.UserControl
{
    private WebPage mWebPage = null;
    public WebPage WebPage { get { return mWebPage; } set { mWebPage = value; } }
 
    public virtual int SlotCount { get { return 0; } }
    public SmartPageBase()
    {
        
    }

    protected override void OnInit(EventArgs e)
    {
        if (!IsPostBack)
        {
            InitializeSmartPage();
        }

        base.OnInit(e);
    }

    private void InitializeSmartPage()
    {
        List<SmartPageComponent> components = SmartPageComponent.List(WebPage.ID);

        for (int i = 0; i < SlotCount; i++)
        {
            SmartPageComponentControlBase smartControl = null;
            HtmlGenericControl slot = (HtmlGenericControl)this.FindControl("slot" + i.ToString());

            if (slot == null)
                continue;

            int editSlot = -1;
            if (Request.QueryString["editSlot"] != null)
                editSlot = int.Parse(Request.QueryString["editSlot"]);

            if (Page is CMSManagementPageBase)
            {
                slot.Attributes.Add("class", "smartpageslot");
            }

            string controlPath = "";
            if (components.Count > i)
            {
                if (components[i].ComponentType == ComponentType.HtmlContent)
                {
                    controlPath = Request.ApplicationPath.TrimEnd('/') + "/smartpage/Components/HtmlContent.ascx";
                }
                else if (components[i].ComponentType == ComponentType.ProductList)
                {
                    controlPath = Request.ApplicationPath.TrimEnd('/') + "/smartpage/Components/ProductList.ascx";
                }
            }
            else
            {
                controlPath = Request.ApplicationPath.TrimEnd('/') + "/smartpage/Components/Empty.ascx";
            }
            smartControl = (SmartPageComponentControlBase)LoadControl(controlPath);

            if (i == editSlot && !IsPostBack)
                smartControl.ChangeMode(FormViewMode.Edit);

            if (smartControl != null)
            {
                smartControl.onChangeComponent += new EventHandler(smartControl_onChangeComponent);
                smartControl.onSaveComponent += new EventHandler(smartControl_onSaveComponent);
                smartControl.WebPage = mWebPage;
                if (components.Count > i)
                    smartControl.SmartComponent = components[i];
                smartControl.SlotIndex = i;
                slot.Controls.Add(smartControl);
            }
        }

    }

    void smartControl_onSaveComponent(object sender, EventArgs e)
    {
        //SmartPageComponentControlBase componentcontrol = (SmartPageComponentControlBase)sender;
        //Response.Redirect("Build.aspx?id=" + componentcontrol.WebPage.ID.ToString());
    }

    void smartControl_onChangeComponent(object sender, EventArgs e)
    {
        //SmartPageComponentControlBase componentcontrol = (SmartPageComponentControlBase)sender;

        //Response.Redirect("Build.aspx?id=" + Request.QueryString["id"] + "&editSlot=" + componentcontrol.SlotIndex.ToString());

        /*
        SmartPageComponent component = componentcontrol.SmartComponent;
        componentcontrol.Visible = false; // disposing of the control will corrupt viewstate
        SmartPageComponentControlBase smartControl = null;

        // redraw the slot with it's correct editor
        if (component.ComponentType == ComponentType.HtmlContent)
        {
            smartControl = (SmartPageComponentControlBase)LoadControl(Request.ApplicationPath.TrimEnd('/') + "/smartpage/Components/HtmlContent.ascx");
        }
        else if (component.ComponentType == ComponentType.ProductList)
        {
            smartControl = (SmartPageComponentControlBase)LoadControl(Request.ApplicationPath.TrimEnd('/') + "/smartpage/Components/ProductList.ascx");
        }

        HtmlGenericControl slot = (HtmlGenericControl)this.FindControl("slot" + component.SlotIndex.ToString());

        if (smartControl != null && slot != null)
        {
            //slot.Controls.Clear();
            smartControl.onChangeComponent += new EventHandler(smartControl_onChangeComponent);
            smartControl.WebPage = mWebPage;
            smartControl.SmartComponent = component;
            smartControl.SlotIndex = component.SlotIndex;
            slot.Controls.Add(smartControl);
            smartControl.ChangeMode(FormViewMode.Edit);
        }
        */
    }
}
